<template>
  <div class="basicInformation">
    <div class="operation">
      <div class="operationLeft">
        <el-input class="inline-input" v-model="userSearchModel.name" @input="nameSearch" placeholder="请输入姓名"
          style="width:220px"></el-input>

        <el-input class="inline-input" v-model="userSearchModel.phone" @input="mobileSearch" placeholder="请输入手机号"
          style="width:220px"></el-input>

        <el-button @click="dialogFormVisible = true">添加教练</el-button>
      </div>
    </div>

    <el-table :data="tableData" style="width: 100%">
      <el-table-column label="姓名" prop="cname"> </el-table-column>
      <el-table-column label="头像">
        <template slot-scope="scope">
          <img :src="scope.row.cimgurl" class="coachImg">
        </template>
      </el-table-column>
      <el-table-column align="center" label="时长单价" prop="cprice"> </el-table-column>
      <el-table-column label="宗旨" prop="contents"> </el-table-column>
      <el-table-column align="center" label="联系电话" prop="cmobile"> </el-table-column>
      <el-table-column align="center" label="等级" prop="clevel"> </el-table-column>
      <el-table-column align="center" label="创建时间" prop="createTime"> </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="text" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="text" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="this.currentPage"
      :page-sizes="[10, 20, 30, 40]" :page-size="this.pageSize" layout="total, sizes, prev, pager, next, jumper"
      :total="this.total" background class="turnPage">
    </el-pagination>

    <el-dialog title="添加教练" :visible.sync="dialogFormVisible">
      <el-form :model="form" label-position="right" label-width="80px">
        <el-form-item label="姓名">
          <el-input v-model="form.cname"></el-input>
        </el-form-item>
        <el-form-item label="头像">
          <el-upload class="avatar-uploader" action="fakeaction" :show-file-list="false" :http-request="uploadFile"
            :before-upload="beforeAvatarUpload">
            <img v-if="form.cimgurl" :src="form.cimgurl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="时长单价">
          <el-input v-model="form.cprice"></el-input>
        </el-form-item>
        <el-form-item label="联系电话">
          <el-input v-model="form.cmobile"></el-input>
        </el-form-item>
        <el-form-item label="等级">
          <el-input v-model="form.clevel"></el-input>
        </el-form-item>
        <el-form-item label="宗旨">
          <el-input v-model="form.contents"></el-input>
        </el-form-item>
        <el-form-item label="简介">
          <el-input v-model="form.jianjie"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCoach">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="编辑教练" :visible.sync="uploadDialog">
      <el-form :model="uploadform" label-position="right" label-width="80px">
        <el-form-item label="姓名">
          <el-input v-model="uploadform.cname"></el-input>
        </el-form-item>
        <el-form-item label="头像">
          <el-upload class="avatar-uploader" action="fakeaction" :show-file-list="false" :http-request="uploadFile2"
            :before-upload="beforeAvatarUpload">
            <img v-if="uploadform.cimgurl" :src="uploadform.cimgurl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="时长单价">
          <el-input v-model="uploadform.cprice"></el-input>
        </el-form-item>
        <el-form-item label="联系电话">
          <el-input v-model="uploadform.cmobile"></el-input>
        </el-form-item>
        <el-form-item label="等级">
          <el-input v-model="uploadform.clevel"></el-input>
        </el-form-item>
        <el-form-item label="宗旨">
          <el-input v-model="uploadform.contents"></el-input>
        </el-form-item>
        <el-form-item label="简介">
          <el-input v-model="uploadform.jianjie"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="uploadDialog = false">取 消</el-button>
        <el-button type="primary" @click="editCoach">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getCoachPerson, addCoach, delCoach, editCoach,uploadFile } from "@/api/getData";
export default {
  name: "UsetList",
  data() {
    return {
      userSearchModel: {
        name: '',
        phone: ''
      },
      tableData: [],
      total: 0,
      currentPage: 1,
      pageSize: 10,
      userId: "",
      dialogFormVisible: false,
      uploadDialog: false,
      form: {
        cname: '',
        cimgurl: '',
        cprice: '',
        cmobile: '',
        clevel: '',
        contents: '',
        jianjie: '',
        province: '北京',
        city: '北京',
        county: '海淀',
        caddress: '中撒旦啊是的发生地方阿萨德国',
        provinceid: 1,
        cityid: 20,
        countyid: 120
      },
      uploadform: {
        cname: '',
        cimgurl: '/upload/2022-04-04/',
        cprice: '',
        cmobile: '',
        clevel: '',
        contents: '',
        jianjie: '',
        province: '北京',
        city: '北京',
        county: '海淀',
        caddress: '中撒旦啊是的发生地方阿萨德国',
        provinceid: 1,
        cityid: 20,
        countyid: 120
      }
    };
  },

  methods: {
    //上传图片
    uploadFile2(params) {
      const file = params.file;
      // 使用FormData传参数和文件
      var param = new FormData();
      // 文件
      param.append("file", file);
      uploadFile(param).then((res) => {
        this.uploadform.cimgurl = res.data
      });
    },
    uploadFile(params) {
      const file = params.file;
      // 使用FormData传参数和文件
      var param = new FormData();
      // 文件
      param.append("file", file);
      uploadFile(param).then((res) => {
        this.form.cimgurl = res.data
      });
    },
    //添加教练
    addCoach() {
      addCoach(this.form).then((res) => {
        if (res.code == 0) {
          this.$message.success('添加成功');
          this.dialogFormVisible = false
          this.getCoachPerson()
        } else {
          this.$message.error('添加失败');
        }
      });
    },
    //输入后匹配
    nameSearch(val) {
      this.userSearchModel.name = val
      this.getCoachPerson()
    },
    mobileSearch(val) {
      this.userSearchModel.phone = val
      this.getCoachPerson()
    },
    beforeAvatarUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isLt2M;
    },
    handleEdit(index, row) {
      this.uploadDialog = true
      this.uploadform.id = row.id
      this.uploadform.cname = row.cname
      this.uploadform.cimgurl = row.cimgurl
      this.uploadform.cprice = row.cprice
      this.uploadform.cmobile = row.cmobile
      this.uploadform.clevel = row.clevel
      this.uploadform.contents = row.contents
      this.uploadform.jianjie = row.jianjie
    },
    editCoach() {
      editCoach(this.uploadform).then((res) => {
        this.$message.success('修改成功');
        this.uploadDialog = false
          this.getCoachPerson()
      });
    },
    handleDelete(index, row) {
      this.$confirm("确认删除该教练吗?", "删除确认", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        delCoach(row.id).then((res) => {
          this.$message.success('删除成功');
          this.getCoachPerson()
        });
      });
    },
    handleSizeChange(val) {
      this.pageSize = val;
    },
    handleCurrentChange(val) {
      this.currentPage = val;
    },
    //教练列表
    getCoachPerson() {
      const params = {
        name: this.userSearchModel.name,
        phone: this.userSearchModel.phone,
        currentPage: this.currentPage,
        pageSize: this.pageSize
      };
      getCoachPerson(params).then((res) => {
        this.tableData = res.data.list
        this.total = res.data.total
      });
    }
  },
  mounted() {
    this.getCoachPerson()
  },
};
</script>

<style lang="scss" scoped>
.accountList-dialog ::v-deep {
  .el-dialog {
    width: 30%;
    max-width: 368px;
    border-radius: 10px;
    overflow: hidden;

    .el-dialog__header {
      padding: 0;
      height: 50px;
      text-align: center;
      background: #ededed;

      .el-dialog__title {
        font-size: 18px;
        color: #8e8e8e;
        line-height: 50px;
      }
    }

    .el-dialog__body {
      padding: 0 60px;

      >div {
        display: flex;
        margin-top: 30px;

        label {
          width: 60px;
          line-height: 44px;
          font-size: 18px;
        }

        .el-input__inner {
          height: 44px;
          width: 100%;
          font-size: 18px;
        }

        .el-select {
          width: 100%;
          margin: 0;
          font-size: 18px;
        }
      }
    }

    .el-dialog__footer {
      text-align: center;
      border-top: 1px solid #ededed;
      margin-top: 30px;
      padding: 30px 0;

      .el-button {
        width: 98px;
      }
    }
  }
}

.basicInformation {
  padding: 0 56px;

  .coachImg {
    width: 100px;
    height: 100px;
  }


  .avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
  border: 1px dashed #d9d9d9;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

  .title {
    font-size: 20px;
    color: #3c3c3c;
    margin: 40px 0;
  }

  .operation {
    display: flex;
    justify-content: space-between;
    margin: 20px 0;

    .operationLeft {
      .el-input {
        margin-right: 20px;
      }
    }

    .operationBtn {
      width: 128px;
      height: 39px;
      background: #24bbd3;
      color: #fff;
      font-size: 18px;
      padding: 0;
    }
  }

  .turnPage {
    text-align: right;
    margin-top: 20px;
  }

  .el-table::v-deep {

    thead th:first-child,
    tbody td:first-child {
      padding-left: 40px;
    }
  }
}
</style>